#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,a,b) for(register int i = (a);i <= (b);++i)
#define per(i,a,b) for(register int i = (a);i >= (b);--i) 
#define mkp std::make_pair
typedef long long ll;
typedef unsigned long long ull;
using std::string;using std::cin;using std::cout;
inline bool cmp(int x,int y){return x < y;}

const int N = 1e6+9;
const int inf = 1e9+9;
const double eps = 1e-7;
int _,n,m,a[2*N],u,v,cnt;
char ch[5];
bool flag[30],already[30],ok,edge[30][30];
std::queue<int> q;

int head[2*N],ver[2*N],next[2*N],deg[2*N],Deg[2*N],tot,p;

inline void link(int x,int y){edge[x][y] = true,ver[++tot] = y , next[tot] = head[x] , head[x] = tot , ++deg[y];}

inline void topsort(){
    while(q.size()) q.pop();
    rep(i,1,26) already[i] = false;
    rep(i,1,26) Deg[i] = deg[i];
    rep(i,1,26) if(deg[i] == 0) q.push(i) , already[i] = true;
    while(q.size()){
        int now = q.front();    q.pop();
        for(int i = head[now];i;i = next[i]) if(--Deg[ ver[i] ] == 0) q.push(ver[i]) , already[ ver[i] ] = true;
    }
    return;
}

inline void dfs(int now,int depth){
    // cout << now << " and dep:  " << depth << "\n"; 
    if(depth == cnt-1){
        ok = true;
        cout << "Sorted sequence determined after " << p << " relations: " << char('A' + now - 1);
        return;
    }   
    for(int i = head[now];i;i = next[i]){
        dfs(ver[i],depth+1);
        if(ok){
            cout << char('A' + now - 1);
            return;
        }
    }
}


int main(){
    std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    #ifdef LOCAL //use "g++ xxx.cpp -DLOCAL"
        freopen("in.in", "r", stdin);
    #endif
    cin >> n >> m;
    rep(i,1,m){
        cin >> ch+1;
        u = ch[1] - 'A' + 1 , v = ch[3] - 'A' + 1;
        if(!flag[u]) ++cnt;
        if(!flag[v]) ++cnt;
        flag[u] = flag[v] = true;
        
        if(!edge[v][u]) link(v,u);

        topsort();
        rep(j,1,26) if(!already[j] && flag[j]){
            cout << "Inconsistency found after " << i << " relations.\n";
            return 0;
        }
        if(cnt < n) continue;
        p = i , deg[0] = 0;
        rep(j,1,26){
            if(deg[j] == 0){
                if(flag[j] && deg[0] == 0) deg[0] = j;
                else if(flag[j]){
                    deg[0] = -1;
                    break;
                }
            }
        }
        if(deg[0] <= 0) continue;
        dfs(deg[0],0);
        if(ok) cout << ".\n";
        if(ok) return 0;

    }
    if(cnt < n){
        cout << "Sorted sequence cannot be determined.\n";
        return 0;
    }
    rep(i,1,26){
        if(deg[i] == 0){
            if(flag[i] && deg[0] <= 0) deg[0] = i;
            else if(flag[i]){
                cout << "Sorted sequence cannot be determined.\n";
                return 0;
            }
        }
    }
    dfs(deg[0],0);
    if(ok) cout << ".\n";
    if(ok) return 0;

    return 0;
}